/**
 * 
 */
package com.UploadClasses;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.Adapters.Items.ItemUpload;
import com.Data.GeoTag;
import com.Data.Post;
import com.Data.Post.onUploadPictureCompleted;
import com.Managers.PictureManagerClass;
import com.Managers.PictureManagerClass.OnCoverCompleted;
import com.Managers.UserManagerClass;
import com.uploadservice.UploadService;


/**
 * @author Chris
 * 
 */
public class UploadManager implements iUploadManager {

	private Context context;
    /*
     * (non-Javadoc)
     * @see
     * com.UploadClasses.iUploadManager#UploadPicture(android.content.Context,
     * java.lang.String[], android.content.Intent)
     */
    @Override
    public void UploadPicture(Activity activity, Post data, ItemUpload[] uploads) {

        Intent mServiceIntent = new Intent(activity, UploadService.class);

        Bundle bund = new Bundle();

        bund.putString("tags", data.getTags());
        boolean hasGeotag = UserManagerClass.UserLogged.isActiveLocation();
        bund.putBoolean("hasGeotag", hasGeotag);

        if (hasGeotag) {

            GeoTag gtag = UserManagerClass.UserLocation;
            bund.putDouble("latitude", gtag.getLatitude());
            bund.putDouble("longitude", gtag.getLongitude());
            bund.putString("city", gtag.getCity());
            bund.putString("country", gtag.getCountry());
            bund.putString("address", gtag.getAddress());

        }
        //
        bund.putInt("idOwner", data.getUser().getID());
        bund.putInt("idUser", UserManagerClass.UserLogged.getID());
        bund.putInt("albumindex", data.getAlbum().getID());
        bund.putInt("privacyindex", data.getPrivate());
        bund.putString("comment", data.getComment());

        boolean[] list = new boolean[uploads.length];
        String[] paths = new String[list.length];

        for (int i = 0; i < list.length; i++) {

            try {

                paths[i] = uploads[i].getItemup().getPathFull();

                list[i] = uploads[i].getItemup().isCover();
            }
            catch (Exception x) {

                list[i] = false;
            }

        }
        bund.putStringArray("pathList", paths);
        bund.putBooleanArray("coverList", list);
        mServiceIntent.putExtras(bund);
        mServiceIntent.putExtra("listArray", list);

        activity.startService(mServiceIntent);

    }

    public String[] convertData(Post data)
    {
    	boolean hasGeotag = UserManagerClass.UserLogged.isActiveLocation();
    	
    	String[] dataup = new String[10];

         if (hasGeotag) {

             dataup = new String[15];
             dataup[10] = String.valueOf(data.getGeoTag().getLatitude());
             dataup[11] = String.valueOf(data.getGeoTag().getLongitude());
             dataup[12] = data.getGeoTag().getAddress();
             dataup[13] = data.getGeoTag().getCity();
             dataup[14] = data.getGeoTag().getCountry();

         }

         if (data.getComment().length() > 0)
			try {
				dataup[0] = URLEncoder.encode(data.getComment(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else
             dataup[0] = "";

         dataup[1] = String.valueOf(data.getUser().getID());
         dataup[2] = String.valueOf(UserManagerClass.UserLogged.getID());
         dataup[3] = "2";
         dataup[4] = "";
         dataup[5] = String.valueOf(data.getAlbum().getID());
         dataup[6] = String.valueOf(data.getPrivate());
         dataup[7] = "";
         dataup[8] = "";
         dataup[9] = "";
         
         return dataup;
    }

    /*
     * (non-Javadoc)
     * @see com.UploadClasses.iUploadManager#UploadText(com.Data.Post)
     */
    @Override
    public void UploadText(Post data) {

    }

	@Override
	public void UploadPictureCover(Post data,Uri image,final Context context,final onUploadPictureCompleted callback) {

	
	PictureManagerClass.getResizedForCover(image, context,new OnCoverCompleted(){

		@Override
		public void Completed(byte[] result) {
			
			String oldCover = UserManagerClass.UserLogged.getCoverPicture();
			
			 if (!oldCover.equals("null")) {
		         int startIndex = oldCover.lastIndexOf("/");
		         int lastIndex = oldCover.length();
		         if (startIndex != 0)
		             oldCover = oldCover.substring(startIndex, lastIndex);

		     }
			
			
			    String[] dat = new String[3];
			    dat[0] = String.valueOf(UserManagerClass.UserLogged.getID());
			    dat[1] ="";
			    dat[2] = oldCover;
			   
			    Post pic = new Post(Post.ACTION_UPLOAD_COVER,context,result);
			    pic.setCallbackUploadPicture(new onUploadPictureCompleted() {

			        @Override
			        public void Completed(String result) {

			            if (callback != null) {

			                callback.Completed(result);
			            }

			        }
			    });
			    pic.execute(dat);
			
		}});
	
	}

	@Override
	public void UploadPicturePorfile(Post data, Uri image, Context context,
			onUploadPictureCompleted callback) {
		// TODO Auto-generated method stub
		
	}
	
	

}
